package org.behrang.macbeans.tab;

import apple.laf.CUIAquaToolBar;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;

/**
 *
 * @author behrangsa
 */
public class MacBeansToolbarUI extends CUIAquaToolBar implements ContainerListener {

    public static ComponentUI createUI(JComponent c) {
        return new MacBeansToolbarUI();

    }

    public void componentAdded(ContainerEvent e) {
        Container src = (Container) e.getSource();
        if (!"editorToolbar".equals(src.getName())) {
            installButtonUI(e.getChild());
        }
    }

    public void componentRemoved(ContainerEvent e) {
    }

    @Override
    public void installUI(JComponent c) {
        super.installUI(c);
        c.addContainerListener(this);
        installButtonUIs(c);
    }

    private void installButtonUI(Component c) {
        if (c instanceof AbstractButton) {
            ((AbstractButton) c).setBorderPainted(false);
            ((AbstractButton) c).setRolloverEnabled(true);
        }
        if (c instanceof JComponent) {
            ((JComponent) c).setOpaque(false);
        }
    }

    private void installButtonUIs(Container parent) {
        if (!"editorToolbar".equals(parent.getName())) {
            Component[] c = parent.getComponents();
            for (int i = 0; i < c.length; i++) {
                installButtonUI(c[i]);
            }
        }
    }
}
